﻿using AutoMapper;
using B.S.Material.Domain.BOM;
using B.S.Material.ErrorCode;
using B.S.Material.Wirth.Api.Applications.Command.MaterialsPickingCommand;
using MediatR;
using SqlSugar;

namespace B.S.Material.Wirth.Api.Applications.Handler.MaterialsPickingHandler
{
    public class UpdateMaterialsPickingHandler : IRequestHandler<UpdateMaterialsPickingCommand, ApiResult<int>>
    {
        private readonly ISqlSugarClient db;
        private readonly IMapper mapper;
        public UpdateMaterialsPickingHandler(ISqlSugarClient db, IMapper mapper)
        {
            this.db = db;
            this.mapper = mapper;
        }
        public async Task<ApiResult<int>> Handle(UpdateMaterialsPickingCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            var model = mapper.Map<MaterialsPicking>(request);
            model.IsDel = false;
            result.data = await db.Updateable<MaterialsPicking>(model).ExecuteCommandAsync();
            if (result.data > 0)
            {
                result.Code = ApiEnum.修改成功;
                result.MAG = ApiEnum.修改成功.ToString();
            }
            else
            {
                result.Code = ApiEnum.修改失败;
                result.MAG = ApiEnum.修改失败.ToString();
            }
            return result;
        }
    }
}
